﻿using Bricks.Business.Models.Account;
using Bricks.Business.Service;
using Bricks.Component.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bricks.Component.Utils.Extensions;
using Bricks.Business.Models.Systems;

namespace Bricks.Site.Web.Controllers
{
    /// <summary>
    /// 数据字典管理Controller
    /// </summary>
    [Export]
    public class DictionaryController : BaseController
    {
        [Import]
        public IDictionaryService DictionaryService { get; set; }

        /// <summary>
        /// 字典类型首页
        /// </summary>
        /// <returns></returns>
        public ActionResult TypeIndex()
        {
            //ViewBag.PanelInfo = base.GetPanelInfo();  //取得面板信息
            ViewBag.ToolBarInfo = base.GetToolBarInfo();  //取得工具栏信息
            return View();
        }

        /// <summary>
        /// 字典首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewBag.ToolBarInfo = base.GetToolBarInfo();  //取得工具栏信息
            return View();
        }

        /// <summary>
        /// 根据字典类型Tag获取字典列表
        /// </summary>
        /// <param name="typeTag"></param>
        /// <returns></returns>
        public ActionResult GetDictionaryByTypeTag(string typeTag)
        {
            var list = DictionaryService.GetDictionaryByTypeTag(typeTag);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        #region 字典类型
        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDictionaryTypeList()
        {
            var queryData = DictionaryService.DictionaryTypes.OrderBy(m => m.OrderNo);  //转换为List之后，可进行函数操作
            return Json(queryData, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DeleteDictionaryType(string id)
        {
            Guid guidID = id.ToGuid();
            var exeResult = DictionaryService.DeleteType(guidID);
            return Json(exeResult);
        }

        /// <summary>
        /// 新增或编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult TypeEdit(string id)
        {
            DictionaryType model = new DictionaryType();
            //如果是编辑
            if (!String.IsNullOrEmpty(id))
            {
                Guid guidID = id.ToGuid();
                var record = DictionaryService.DictionaryTypes.FirstOrDefault(m => m.ID == guidID);
                if (record != null)
                {
                    model = record;
                }
            }
            return View(model);
        }

        /// <summary>
        /// 新增或编辑（响应）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult TypeEditPost(DictionaryType model)
        {
            var result = new OperationResult(OperationResultType.NoChanged);
            var record = DictionaryService.DictionaryTypes.FirstOrDefault(m => m.ID == model.ID);

            //新增
            if (record == null)
            {
                result = DictionaryService.AddType(model);
                //if (result.ResultType == OperationResultType.Success)
                //    return View(result.AppendData);
            }
            //修改
            else
            {
                result = DictionaryService.ModifyType(model);
                //if (result.ResultType == OperationResultType.Success)
                //    return View(result.AppendData);
            }
            //return View();
            return Json(result, "text/html");
        }

        /// <summary>
        /// 排序设置
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="order">排序</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SetOrderDictionaryType(string id, string order)
        {
            Guid guidID = id.ToGuid();
            var exeResult = DictionaryService.SetOrderType(guidID, order);
            return Json(exeResult);
        }
        #endregion

        #region 字典
        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDictionaryList(string dictionaryTypeID)
        {
            IQueryable<Dictionary> queryData = null;  //转换为List之后，可进行函数操作
            if (!String.IsNullOrEmpty(dictionaryTypeID))
            {
                Guid dictionaryTypeGuid = dictionaryTypeID.ToGuid();
                queryData = DictionaryService.Dictionarys.Where(m => m.DictionaryTypeID == dictionaryTypeGuid).OrderBy(m => m.OrderNo);
            }
            return Json(queryData, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 启用或禁用
        /// </summary>
        /// <returns></returns>
        public ActionResult UsedDictionary()
        {
            string id = Request["ID"];
            string isUsed = Request["IsUsed"];

            Guid guidID = Guid.Parse(id);
            var record = DictionaryService.Dictionarys.FirstOrDefault(m => m.ID == guidID);
            record.IsDeleted = isUsed == "false" ? true : false;

            var result = DictionaryService.ModifyDictionary(record);

            if (result.ResultType == OperationResultType.Success)
                return View(result.AppendData);

            return Content(id + isUsed);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DeleteDictionary(string id)
        {
            Guid guidID = id.ToGuid();
            var exeResult = DictionaryService.DeleteDictionary(guidID);
            return Json(exeResult);
        }

        /// <summary>
        /// 新增或编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(string id, string dictionaryTypeID)
        {
            Dictionary model = new Dictionary();
            //如果是编辑
            if (!String.IsNullOrEmpty(id))
            {
                Guid guidID = id.ToGuid();
                var record = DictionaryService.Dictionarys.FirstOrDefault(m => m.ID == guidID);
                if (record != null)
                {
                    model = record;
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(dictionaryTypeID))
                {
                    Guid dictionaryTypeGuid = dictionaryTypeID.ToGuid();
                    model.DictionaryTypeID = dictionaryTypeGuid;
                }
            }
            return View(model);
        }

        /// <summary>
        /// 新增或编辑（响应）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditPost(Dictionary model)
        {
            var result = new OperationResult(OperationResultType.NoChanged);
            var record = DictionaryService.Dictionarys.FirstOrDefault(m => m.ID == model.ID);

            //新增
            if (record == null)
            {
                result = DictionaryService.AddDictionary(model);
                //if (result.ResultType == OperationResultType.Success)
                //    return View(result.AppendData);
            }
            //修改
            else
            {
                result = DictionaryService.ModifyDictionary(model);
                //if (result.ResultType == OperationResultType.Success)
                //    return View(result.AppendData);
            }
            //return View();
            return Json(result, "text/html");
        }

        /// <summary>
        /// 设置禁用或启用
        /// </summary>
        /// <param name="id">记录ID</param>
        /// <param name="isEnable">是否启用</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SetEnableDictionary(string id, string isEnable)
        {
            var result = new OperationResult(OperationResultType.NoChanged);
            Guid guidID = id.ToGuid();
            var record = DictionaryService.Dictionarys.FirstOrDefault(m => m.ID == guidID);
            if (record != null)
            {
                record.IsDeleted = isEnable == "False";
                //UserView view = new UserView();
                //view = record.CopyTo<UserView>();
                result = DictionaryService.ModifyDictionary(record);
                if (result.ResultType == OperationResultType.Success)
                    result.Message = (isEnable == "True" ? "启用" : "禁用") + "成功";
            }
            return Json(result);
            //return View();
        }

        /// <summary>
        /// 排序设置
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="order">排序</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SetOrderDictionary(string id, string order)
        {
            Guid guidID = id.ToGuid();
            var exeResult = DictionaryService.SetOrderDictionary(guidID, order);
            return Json(exeResult);
        }
        #endregion

        #region DBViewModel操作
        /// <summary>
        /// 获取数据库中的视图字典
        /// </summary>
        /// <returns></returns>
        public ActionResult GetVMDictionaryInfo()
        {
            var list = DictionaryService.GetVMDictionaryInfo();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
